package com.tiantian.novel.resolver.impl;

import com.alibaba.fastjson.JSON;
import com.tiantian.novel.helper.HttpHelper;
import com.tiantian.novel.helper.PinYinUtil;
import com.tiantian.novel.model.ChapterInfo;
import com.tiantian.novel.model.NovelSummary;
import com.tiantian.novel.resolver.NovelResolver;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.stream.Collectors;

/**
 * @author wangk21
 * @date 2023/11/26.
 */
public class Kline8RovelResolver implements NovelResolver {
    private static final Logger log = LoggerFactory.getLogger(BiqugeNewNovelResolver.class);
    /**
     * http utils
     */
    private HttpHelper http;

    public Kline8RovelResolver() {
        this.http = new HttpHelper(true);
    }

    @Override
    public NovelSummary resolveNovelSummary(String url) throws Exception {
        // https://www.kline8.com/chanlun?p=0
        log.info("resolve NovelInfo from www 【{}】", url);
        Element body = http.getBody(url);
        // 标题、作者、更新状态、最后更新时间
        Element info = body.selectFirst("div.container-narrow");
        String title = "教你炒股票108课";
        String author = "缠中说禅";
        String updateStatus = "完结";
        String updateTime = "2008-08-29";
        String logo = "https://pics3.baidu.com/feed/63d0f703918fa0ec8ad9fc837a39fde23c6ddb59.jpeg@f_auto?token=acb541206cfe34c17c802d608646f479";
        NovelSummary novel = new NovelSummary(PinYinUtil.getShortPinYin(title), url, title, author, updateStatus,
                                              updateTime, logo);
        // 目录
        Elements links = info.select("p a.chan");
        int index = 1;
        for (Element link : links) {
            novel.add(new ChapterInfo(index++, link.attr("abs:href"), link.text()));
        }
        log.info("novelSummary:{}", JSON.toJSONString(novel));
        return novel;
    }

    @Override
    public ChapterInfo resovleChapter(String url) throws Exception {
        log.info("resovle ChapterInfo from www 【{}】", url);
        Element body = http.getBody(url);
        if (body == null) {
            log.info("body is null");
            return null;
        }
        Element title = body.selectFirst("div.container-narrow>h2");
        if (title == null) {
            log.info("title is null");
            return null;
        }

        Element container = body.selectFirst("div.container-narrow");
        if (container == null) {
            log.info("container is null");
            return null;
        }

        String contentText = container.select("p")
                .stream()
                .map(p -> {
                    // 目录
                    if (p.hasClass("mt-5")) {
                        return "";
                    } else {
                        return String.format("<p>%s</p>\n", p.text().trim());
                    }
                })
                .filter(StringUtils::hasText)
                .collect(Collectors.joining(""));
        ChapterInfo info = new ChapterInfo(0, url, title.text().trim());
        info.setContent(contentText);
        return info;
    }

    @Override
    public byte[] getImage(String logo) throws IOException {
        return http.getImage(logo);
    }
}
